﻿using UnityEngine;
using System.Collections;
using UICore;
using UnityEngine.UI;
using System.Collections.Generic;

public class Player : MonoBehaviour {

    public float speed = 10;
    GameObject effectPre;
    //声明子弹预制体
    GameObject bulletPre;
    //声明子弹生成的位置
    Transform bulletStartPos;


	void Start () {
        bulletPre = Resources.Load<GameObject>("Bullet");
        bulletStartPos = transform.Find("BulletStartPos");
        effectPre = Resources.Load<GameObject>("explosion_player");
	}
	

	void Update () {

        Explode();
        Move();
        Attack(); 
 
	}

    //玩家操控移动的方法
    void Move()
    {
        //获取键盘水平轴的信息
        float h = Input.GetAxis("Horizontal");
        //左右移动胡范围在 -13.5，13.5之间
        if (h < 0 && transform.position.x > -13.5 )
        {
            transform.position = transform.position + new Vector3(h, 0, 0) * speed * Time.deltaTime;
            transform.rotation = Quaternion.Euler(0, 0, 30);

        }
        if( h > 0 && transform.position.x < 13.5)
        {
             transform.position = transform.position + new Vector3(h, 0, 0) * speed * Time.deltaTime;
             transform.rotation = Quaternion.Euler(0, 0, -30);
    
        }
        //获取键盘垂直轴的信息
        float v = Input.GetAxis("Vertical");
        //上下移动胡范围在-16.5，-4.5 之间
        if (v < 0 && transform.position.z > -16.5 || v > 0 && transform.position.z < -4.5)
        {
            transform.position = transform.position + new Vector3(0, 0, v) * speed * Time.deltaTime;
        }


    }

    void Attack()
    {
        //按下左键的时候，生成子弹并发射
        if (Input.GetMouseButtonDown(0))
        {
            Instantiate(bulletPre,bulletStartPos.position,bulletPre.transform.rotation);
        }
    }


    //陨石，敌方子弹全部爆炸
    void Explode()
    {
        if(UIManager.Instance.GetUIPanel(E_UiId.PlayUI).GetComponent<PlayUI>().GetSliderValue()==1.0f)
        {
        if (Input.GetKeyDown(KeyCode.Space))
        {
           GameObject[] allFoeBullet=GameObject.FindGameObjectsWithTag("FoeBullets");
           GameObject[] allEnemy = GameObject.FindGameObjectsWithTag("Enemy");
           for (int i = 0; i < allFoeBullet.Length; i++)
           {
               Destroy(allFoeBullet[i]);
               

           }
           for (int i = 0; i < allEnemy.Length; i++)
           {
               Destroy(allEnemy[i]);
              

           }
           Instantiate(effectPre, this.transform.position, Quaternion.identity);

           //减少蓝条
           UIManager.Instance.GetUIPanel(E_UiId.PlayUI).GetComponent<PlayUI>().ReduceBlue(1.0f);
      
        }  
        }

    }



    //如果发生碰撞
    void OnCollisionEnter(Collision other)
    {
        //飞机撞陨石
        if (other.gameObject.tag == "Enemy")
        {
           //血量不够0.2
            if (UIManager.Instance.GetUIPanel(E_UiId.PlayUI).GetComponent<PlayUI>().GetSlider1Value() <= 0.2f)
            {
                UIManager.Instance.GetUIPanel(E_UiId.PlayUI).GetComponent<PlayUI>().SetSlider1Value();//设置红条为0
                Destroy(this.gameObject);//销毁飞机
                Destroy(other.gameObject);//销毁陨石
                Instantiate(effectPre, this.transform.position, Quaternion.identity);
                PlayUI.NoPassLevel(); //未通关
            }
            //血量足够
            else
            {
                Instantiate(effectPre, this.transform.position, Quaternion.identity);
                //减少红条
                UIManager.Instance.GetUIPanel(E_UiId.PlayUI).GetComponent<PlayUI>().ReduceRed(0.2f);

                //判断减完后是否无血
                if (UIManager.Instance.GetUIPanel(E_UiId.PlayUI).GetComponent<PlayUI>().GetSlider1Value() <= 0)
                {
                    Destroy(this.gameObject);//销毁飞机
                    Destroy(other.gameObject);//销毁陨石
                    PlayUI.NoPassLevel(); //未通关
                }
            } 
        }

        //飞机撞子弹
        if (other.gameObject.tag == "FoeBullets")
        {
            //血量不够0.1
            if (UIManager.Instance.GetUIPanel(E_UiId.PlayUI).GetComponent<PlayUI>().GetSlider1Value() <= 0.1f)
            {
                UIManager.Instance.GetUIPanel(E_UiId.PlayUI).GetComponent<PlayUI>().SetSlider1Value();//设置红条为0
                Destroy(this.gameObject);//销毁飞机
                Destroy(other.gameObject);//销毁敌人子弹
                Instantiate(effectPre, this.transform.position, Quaternion.identity);
                PlayUI.NoPassLevel(); //未通关
            }

              //血量足够
            else
            {
                Instantiate(effectPre, this.transform.position, Quaternion.identity);
                //减少红条
                UIManager.Instance.GetUIPanel(E_UiId.PlayUI).GetComponent<PlayUI>().ReduceRed(0.1f);

                //判断减完后是否无血
                if (UIManager.Instance.GetUIPanel(E_UiId.PlayUI).GetComponent<PlayUI>().GetSlider1Value() <= 0)
                {
                    Destroy(this.gameObject);//销毁飞机
                    Destroy(other.gameObject);//销毁子弹
                    PlayUI.NoPassLevel(); //未通关
                }
            } 
        }
    }
}
